package com.nescafe.nescafepromo.entity;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.nescafe.nescafepromo.util.FormatUtil;

public class Participant extends NPEntity {

	public static final String FB_ID_FIELD = "facebookID";
	public static final String NAME_FIELD = "name";
	public static final String EMAIL_FIELD = "email";
	public static final String LOCATION_FIELD = "location";

	private String email = null;
	private String facebookID = null;
	private String name = null;
	private String location = null;

	private TotalPoints totalPoints = null;
	private List<ExtraPoint> extraPoints = new ArrayList<ExtraPoint>();

	public Participant(String email) {
		this.email = email;
	}

	public Participant(Entity entity) {
		this.email = entity.getKey().getName();
		this.facebookID = (String) entity.getProperty(FB_ID_FIELD);
		this.name = (String) entity.getProperty(NAME_FIELD);
		this.location = (String) entity.getProperty(LOCATION_FIELD);
	}

	public String getFacebookID() {
		return facebookID;
	}

	public String getName() {
		return name;
	}
	public String getEmail() {
		return email;
	}

	public String getLocation() {
		return location;
	}

	public void setFacebookID(String facebookID) {
		this.facebookID = facebookID;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getDisplayLocation() {
		return location != null ? location : "";
	}

	public String getDisplayName() {
		return name != null ? name : email;
	}

	public String getDisplayFBID() {
		return facebookID != null ? facebookID : "N/A";
	}

	public String getDisplayTotalPoints() {
		if (totalPoints == null)
			return "0";
		return FormatUtil.totalPointsFormat.format(totalPoints.getTotalPonts());
	}

	public void setTotalPoints(TotalPoints totalPoints) {
		this.totalPoints = totalPoints;
	}

	public TotalPoints getTotalPoints() {
		return totalPoints;
	}

	public List<ExtraPoint> getExtraPoints() {
		return extraPoints;
	}

	public void addExtraPoint(ExtraPoint extraPoint) {
		extraPoints.add(extraPoint);
	}

	@Override
	public Entity toEntity() {
		Entity e = new Entity(EntityType.PARTICIPANT.getKind(), email);
		if (facebookID != null && !facebookID.isEmpty()) {
			e.setUnindexedProperty(FB_ID_FIELD, facebookID);
		}

		if (name != null && !name.isEmpty()) {
			e.setUnindexedProperty(NAME_FIELD, name);
		}
		if (location != null && !location.isEmpty()) {
			e.setUnindexedProperty(LOCATION_FIELD, location);
		}
		return e;
	}

}
